package kr.co.insoft.core.service.callback.impl;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;

import kr.co.insoft.core.model.callback.CallBackDataModel;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import flexjson.JSONDeserializer;

public class CallBackDataConvert implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2533383890513834264L;

	private static final Logger CallBackDataConvertLog = LoggerFactory
			.getLogger(CallBackDataConvert.class);

	final String callBackData;

	public CallBackDataConvert(String callBackData) {
		this.callBackData = callBackData;
	}

	public CallBackDataModel doConvert() {
		CallBackDataModel callBackDataModel = new CallBackDataModel();

		if (!StringUtils.hasLength(callBackData)) {
			return callBackDataModel;
		}
		
		JSONDeserializer<HashMap<String, Object>> deserializer = new JSONDeserializer<HashMap<String, Object>>();
		try {
			BeanUtils.populate(callBackDataModel,
					deserializer.deserialize(callBackData));
		} catch (IllegalAccessException | InvocationTargetException e) {
			if (CallBackDataConvertLog.isDebugEnabled()) {
				CallBackDataConvertLog.debug(
						"((((( Exception Target CallBackData )))))\n{}",
						callBackData);
			}
		}
		return callBackDataModel;

	}

}
